﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Unity;
using BBInterfaceNET.Services;
using BBInterfaceNET.Events.Commands;
using BBInterfaceNET.CommonServices;
using Microsoft.Practices.Prism.Modularity;
using ExplorerInfrastructure;
namespace BBInterfaceNET
{
    public class Bootstrapper:UnityBootstrapper
    {
        protected override IModuleCatalog CreateModuleCatalog()
        {
            ConfigurationModuleCatalog catalog = new ConfigurationModuleCatalog();
            return catalog;
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            this.Container.RegisterInstance<IDictionaryMergingService>(new DictionaryMergingService(App.Current.Resources));
            Container.RegisterInstance<IInteractionService>(new InteractionService());
            Container.RegisterInstance<IDesignerPersistenceService>(new FileDesignerPersistenceService());
            Container.RegisterInstance<IConfirmationService>(new InteractionService());
            Container.RegisterInstance<ICodeGenerationService>(new CodeGenerationService());
        }

        protected override System.Windows.DependencyObject CreateShell()
        {
            return new Shell();
        }
        protected override void InitializeShell()
        {
            base.InitializeShell();

            Shell shell = (Shell)this.Shell;
            ShellViewModel vm = Container.Resolve<ShellViewModel>();
            shell.DataContext = vm;
            vm.Shutdown += (s, e) => {
                shell.Close();
            };
            shell.Closing += (s, e) => {
                if (InfrastructureCommands.ShutdownCommand.CanExecute(e))
                    InfrastructureCommands.ShutdownCommand.Execute(e);
                if (e.Cancel)
                {
                    //display the dialog to ask for directions
                    IInteractionService intService = Container.Resolve<IInteractionService>();
                    intService.ShowConfirmationDialog("Shutdown", "There are still some unsaved documents. Do you want to save them before closing?",
                        (res) => {
                            if (res!=null && res.Value)
                            {//save and exit
                                if (InfrastructureCommands.SaveAllCommand.CanExecute(null))
                                    InfrastructureCommands.SaveAllCommand.Execute(null);
                                e.Cancel = false;
                            }
                            else if (res!=null && !res.Value)
                            {//don't save and exit
                                e.Cancel = false;
                            }
                        });
                }
            };
            App.Current.MainWindow = shell;
            UIDispatcher dispatcher = new UIDispatcher(shell.Dispatcher);
            this.Container.RegisterInstance<IDispatcherService>(dispatcher);
            shell.Show();
        }

    }
}
